package com.common.utils;

import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author davexin
 */

@Component
public class LoginInterceptor implements HandlerInterceptor {

	private static final Logger log = Logger.getLogger(LoginInterceptor.class);

	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
			Exception arg3) throws Exception {
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
			ModelAndView arg3) throws Exception {
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		//请求的路径
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		String  url  =  "http://"  +  request.getServerName()  +  ":"  +  request.getServerPort()  +  request.getContextPath() + request.getServletPath(); //.substring(0,request.getServletPath().lastIndexOf("/")+1);
		HttpSession session = request.getSession();
		String user = (String) session.getAttribute(SystemConstants.SESSIONNAME);
		Gson gson = new Gson();
		Map<String, Object> parames = null;
		log.info("------******登录拦截器 start ******------url="+url);
		//这里可以根据session的用户来判断角色的权限，根据权限来重定向不同的页面，简单起见，这里只是做了一个重定向, 不做请求信息日志输出；
		//********* 20201218 shanshu
		if (StringUtils.isEmpty(user)) {
			//被拦截，重定向到login界面
			//log.error("------*******LoginInterceptor拦截到未登录的非法请求action方法的ip是="+request.getRemoteAddr());
			//log.error("------拦截跳转到前台登录页：/WEB-INF/views/business/t_company_user/company_logon.jsp");
			//response.sendRedirect(basePath+"login"); //此行代码存在问题，不能使用
			log.info("------******登录拦截器输出 LoginInterceptor 由于未登录，客户端即将跳转至 url ="+basePath +"htlogin");
			//request.getRequestDispatcher(basePath+"htlogin").forward(request, response);
			//response.sendRedirect("/htlogin");

			request.getRequestDispatcher("/htlogin").forward(request, response);

			//request.getRequestDispatcher("/WEB-INF/views/login/login.jsp").forward(request, response);
			//request.getRequestDispatcher("/WEB-INF/views/business/t_company_user/company_logon.jsp").forward(request, response);
			log.info("------******登录拦截器输出 end ******------"+url);
			return false;
		} //*******/
		else{
			log.info("------******登录拦截器输出LoginInterceptor 放行，不拦截，'"+user+"'用户已经登录,合法请求!");
			//如果是登录了访问了无效地址；则跳转至默认页面;
			if(url.contains("error")||url.contains("ERROR")){
				request.getRequestDispatcher("/ERROR").forward(request, response);
				log.info("------******登录拦截器输出 end ******------"+url);
				return false;
			}else{
				log.info("------******登录拦截器输出 end ******------"+url);
				return true;
			}
		}
	}

}
